﻿@using ZBJF.Warranty.Common.Enums.InsuranceWarranty
@using ZBJF.Warranty.Model
@using ZBJF.Warranty.WebProvider
@using EnumHelper = ZBJF.Warranty.Common.Common.EnumHelper
@model ZBJF.Warranty.Model.InsuranceWarrantyRecord
@section Header{
    @Scripts.Render("~/bundles/datatable")
    @Styles.Render("~/Content/datatable")
}
@{
    var warrantyMaintenanceType = Model.WarrantyMaintenanceTypeID;
}
<script src="~/Scripts/WarrantyRecord/warrantyRecord.js"></script>

<h4>重新安排维修</h4>
@if (warrantyMaintenanceType == (byte)WarrantyMaintenanceType.SendRevised)
{
    <div id="serviceProviderBranchMain">
        <div class="search-panel">
            <select name="WarrantyMaintenanceTypeID" id="WarrantyMaintenanceTypeID">
                <option value="@warrantyMaintenanceType">@(EnumHelper.Instance.GetDescription(typeof(WarrantyMaintenanceType), warrantyMaintenanceType))</option>
            </select>
            <label>配件</label>
            @Html.DropDownList("AccessoriesTypeID", ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.AccessoriesTypeConvertToSelectList(Model.InsuranceOrder.ProductTypeId??0,addEmptySelect: false))
            <label>配件品质</label>
            @Html.DropDownList("AccessoriesLevelID", ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.AccessoriesLevelConvertToSelectList(addEmptySelect: false))
            <label>所在省</label>
            @Html.DropDownList("SortingCenterBelongProvinceID", ConvertToSelector.Instance.ProvinceConvertToSelectList(selectedValue: Model.SortingCenter.ProvinceId), new { disabled = "disabled" })
            <label>所在市</label>
            @Html.DropDownList("SortingCenterBelongCityID", ConvertToSelector.Instance.D_CityConvertToSelectList(Model.SortingCenter.ProvinceId, selectedValue: Model.SortingCenter.CityId), new { disabled = "disabled" })
            <input type="button" class="btn btn-primary" value="筛选" onclick="searchProviderBranch()" />
        </div>
        @Html.HiddenFor(m => m.SortingCenterID)
        @Html.HiddenFor(m => m.ServiceProviderBranchId)
        <table class="table table-responsive table-bordered table-hover" id="tabServiceProviderBranch">
            <thead>
                <tr>
                    <th>选择</th>
                    <th>所属服务商</th>
                    <th>网点名称</th>
                    <th>维修报价</th>
                    <th>所在省</th>
                    <th>所在市</th>
                    <th>详细地址</th>
                </tr>
            </thead>
        </table>
    </div>
    <script type="text/javascript">
        $(function () {
            $("#tabServiceProviderBranch")
                .dataTable({
                    "sAjaxSource": "/WorkOrderCommonInfo/ServiceProviderBranchList?AccessoriesTypeID=" +
                        $("#AccessoriesTypeID").val() +
                        "&AccessoriesLevelID=" +
                        $("#AccessoriesLevelID").val() +
                        "&provinceId=" +
                        $("#SortingCenter_ProvinceId").val() +
                        "&CityId=" +
                        $("#SortingCenter_CityId").val() +
                        "&ServiceType=" +
                        $("#WarrantyMaintenanceTypeID").val() +
                        "&ProductModelId=@Model.InsuranceOrder.ProductModelId",
                    aoColumns: [
                        {
                            "mData": null,
                            "bSortable": false,
                            "fnRender": function (nRow) {
                                return '<input type="radio" name="ServiceProviderBranch" class="CheckBoxItem" ' +
                                    ("@(Model.ServiceProviderBranchId)" == nRow.aData.ServiceProviderBranchID
                                        ? "checked"
                                        : "") +
                                    ' value="' +
                                    nRow.aData.ServiceProviderBranchID +
                                    '" onclick="checkServiceBranch(this)"/>';
                            }
                        },
                        { "mData": "ServiceProviderName", "bSortable": false },
                        { "mData": "ServiceProviderBranchName", "bSortable": false },
                        { "mData": "QuotationPrice", "bSortable": false },
                        { "mData": "ProvinceName", "bSortable": false },
                        { "mData": "CityName", "bSortable": false },
                        { "mData": "Address", "bSortable": false }
                    ]
                });
        });
        function Save() {
            var param = {};
            var sortingRemark = $("#SortingRemark").val();
            var accessoriesTypeID = $("#AccessoriesTypeID").val();
            var accessoriesLevelID = $("#AccessoriesLevelID").val();
            var obj = $("#tabServiceProviderBranch").find(":radio:checked");
            var serviceProviderBranchId = $("#ServiceProviderBranchId").val();
            var serviceProviderName = $(obj).parent().siblings().eq(0).html();
            var serviceProviderBranchName = $(obj).parent().siblings().eq(1).html();
            var quotationPrice = $(obj).parent().siblings().eq(2).html();


            param.Status = "@Model.Status";
            param.SortingRemark = sortingRemark;
            param.AccessoriesTypeID = accessoriesTypeID;
            param.AccessoriesLevelID = accessoriesLevelID;
            param.InsuranceWarrantyRecordID = "@Model.ID";
            param.ServiceProviderBranchId = serviceProviderBranchId;
            param.ServiceProviderName = serviceProviderName;
            param.ServiceProviderBranchName = serviceProviderBranchName;

            if (serviceProviderBranchId == "") {
                layer.alert("请选择维修服务商");
                return;
            }
            if (parseFloat(quotationPrice) > parseFloat("@Model.InsuranceOrder.InsuredAmount")) {//验证报价是否超过保额
                layer.alert("维修报价大于保额@(Model.InsuranceOrder.InsuredAmount)，请选择其他维修商");
                return;
            }
            layer.load();
            $.ajax({
                url: "/InMaintainWorkOrder/SaveSendOrderAgain?ID=@Model.ID",
                type: "Post",
                data: param,
                success: function (data) {
                    layer.alert(data.Message, {
                        end: function () {
                            layer.closeAll();
                            if (data.Success) {
                                layer.load();
                                location.reload();
                            }
                        },
                        yes: function () {
                            layer.closeAll();
                            if (data.Success) {
                                layer.load();
                                location.reload();
                            }
                        }
                    });
                }
            });
        }
    </script>
}
else
{
    <label>选择维修方式</label>
    @Html.DropDownListFor(m => m.WarrantyMaintenanceTypeID, ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(WarrantyMaintenanceType), (byte)WarrantyMaintenanceType.SendRevised))
    <p>
        <label style="width: 65%;">请在下方输入客户地址信息:</label><input type="button" class="float-right" value="呼叫快递" id="CallCurier" />
    </p>
    <table class="table table-responsive table-bordered table-hover">
        <tr>
            <th class="w120">报案人：</th>
            <td class="w-p25">@Html.DisplayFor(o => o.Reporter)</td>
            <th class="w120">报案人电话：</th>
            <td class="w-p25">@Html.DisplayFor(o => o.ReporterPhone)</td>
        </tr>
        <tr>
            <th class="w120">报案人地址：</th>
            <td colspan="3">@Html.TextBoxFor(o => o.AccidentAddress, new { @class = "w-p98" })</td>
        </tr>
    </table>
    <div id="sortingCenterMain">
        <h5>请在下方选定分拣中心</h5>
        <label>所在省</label>
        @Html.DropDownList("SortingCenterBelongProvinceID", ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.D_ProvinceConvertToSelectList(), new { @class = "Province" })
        <label>所在市</label>
        <select name="SortingCenterBelongCityID" id="SortingCenterBelongCityID">
            <option>先选择省份</option>
        </select>
        <input type="button" class="btn" value="重置" onclick="resetSortingCenter()" />
        <input type="button" class="btn btn-primary" value="查询" onclick="searchSortingCenter()" />
        <table class="table table-responsive table-bordered table-hover" id="tabSortingCenter">
            <thead>
                <tr>
                    <th>选择</th>
                    <th>中心名称</th>
                    <th>所在省</th>
                    <th>所在市</th>
                    <th>详细地址</th>
                    <th>联系人</th>
                    <th>联系电话</th>
                </tr>
            </thead>
        </table>
        <form id="fromExpressInfo" enctype="multipart/form-data" method="post">
            @{
    var insuranceWarrantyRecordExpressInfo = Model.InsuranceWarrantyRecordExpressInfo.FirstOrDefault(o => o.ExpressSendByWhoType == (byte)ExpressSendByWhoType.UserSendToPartitionCenter) ?? new InsuranceWarrantyRecordExpressInfo();
            }
            @Html.HiddenFor(m => m.SortingCenterID)
            @Html.HiddenFor(m => m.ServiceProviderBranchId)
            <h4 class="w-p60 float-left">物流信息</h4>
            <p class="float-right">
                <input type="button" value="获取报价" /><input type="button" value="快递下单" />
            </p>
            <table class="table table-responsive table-bordered table-hover">
                <tr>
                    <th class="w120">寄件人：</th>
                    <td class="w-p25">@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.SendUserName, new { Value = string.IsNullOrEmpty(insuranceWarrantyRecordExpressInfo.SendUserName) ? Model.Reporter : insuranceWarrantyRecordExpressInfo.SendUserName, validate = "{CanNotContainsSpecialChar:true,required:true,messages:{required:'寄件人不能为空'}}" })</td>
                    <th class="w120">寄件人电话：</th>
                    <td class="w-p25">@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.SendUserPhone, new { Value = string.IsNullOrEmpty(insuranceWarrantyRecordExpressInfo.SendUserPhone) ? Model.ReporterPhone : insuranceWarrantyRecordExpressInfo.SendUserPhone, validate = "{isTelphone:true,required:true,messages:{required:'寄件人电话不能为空'}}" })</td>
                </tr>
                <tr>
                    <th>寄件人地址：</th>
                    <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.SendUserAddress, new { validate = "{required:true,messages:{required:'寄件人地址不能为空'}}" })</td>
                    <th>收件人：</th>
                    <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserName, new { validate = "{required:true,messages:{required:'收件人不能为空'}}" })</td>
                </tr>
                <tr>
                    <th>收件人电话：</th>
                    <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserPhone, new { validate = "{isTelphone:true,required:true,messages:{required:'收件人电话不能为空'}}" })</td>
                    <th>收件人地址：</th>
                    <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserAddress, new { validate = "{required:true,messages:{required:'收件人地址不能为空'}}" })</td>
                </tr>
                <tr>
                    <th>快递公司：</th>
                    <td>@Html.DropDownListFor(n => insuranceWarrantyRecordExpressInfo.ExpressCompanyID, ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(ExpressEnums), 1))</td>
                    <th>快递价格：</th>
                    <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ExpressPrice, new { maxlength = 7, onkeyup = "onlyInputDecimal(this,2)", validate = "{max:9999.99,number:true}" })</td>
                </tr>
                <tr>
                    <th>快递单号：</th>
                    <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ExpressNo, new { validate = "{isRightfulStringNounderline:true,required:true,messages:{required:'快递单号不能为空'}}" })</td>
                    <th>取件快递员手机：</th>
                    <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.PickUpExpressManPhone, new { validate = "{isTelphone:true}" })</td>
                </tr>
                <tr>
                    <th>取件快递员：</th>
                    <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.PickUpExpressManName)</td>
                    <th>物流状态：</th>
                    <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ExpressStatus)</td>
                </tr>
            </table>
            <h4>分拣备注</h4>
            @Html.TextAreaFor(n => n.SortingRemark, new { @class = "w-p98", placeholder = "请填写分拣备注" })
        </form>
    </div>
    <div id="serviceProviderBranchMain">
        <div class="search-panel">
            <h5>维修安排：请在下方选定一家需使用的服务商</h5>
            <label>配件</label>
            @Html.DropDownList("AccessoriesTypeID", ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.AccessoriesTypeConvertToSelectList(Model.InsuranceOrder.ProductTypeId ?? 0, addEmptySelect: false))
            <label>配件品质</label>
            @Html.DropDownList("AccessoriesLevelID", ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.AccessoriesLevelConvertToSelectList(addEmptySelect: false))
            <label>所在省</label>@Html.DropDownList("ProviderBranchProvinceID", ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.D_ProvinceConvertToSelectList(addEmptySelect: false), new { @class = "Province" })
            <label>所在市</label>
            <select name="ProviderBranchCityID" id="ProviderBranchCityID"></select>
            <input type="button" class="btn btn-primary" value="查询" onclick="searchProviderBranch()" />
        </div>
        <table class="table table-responsive table-bordered table-hover" id="tabServiceProviderBranch">
            <thead>
                <tr>
                    <th>选择</th>
                    <th>所属服务商</th>
                    <th>网点名称</th>
                    <th>维修报价</th>
                    <th>所在省</th>
                    <th>所在市</th>
                    <th>详细地址</th>
                </tr>
            </thead>
        </table>
    </div>
    <div id="userWarrantyRemarkMain">
        <h4>维修备注</h4>
        @Html.TextAreaFor(n => n.UserWarrantyRemark, new { @class = "w-p98", placeholder = "请填写维修备注" })
    </div>
    <script type="text/javascript">
        $(function () {
            changeMaintenanceType($("#WarrantyMaintenanceTypeID")); //切换维修方式
            //加载分拣中心
            $("#tabSortingCenter").dataTable({
                "sAjaxSource": "/WorkOrderCommonInfo/SortingCenterList",
                aoColumns: [
                    {
                        "mData": null,
                        "bSortable": false,
                        "fnRender": function (nRow) {
                            return '<input type="radio" name="SortingCenter" class="CheckBoxItem" ' + ("@(Model.SortingCenterID)" == nRow.aData.ID ? "checked" : "") + ' value="' +
                                nRow.aData.ID + '" onclick="checkSortingCenter(this)"/>';
                        }
                    },
                    { "mData": "SortingCenterName", "bSortable": false },
                    { "mData": "ProvinceName", "bSortable": false },
                    { "mData": "CityName", "bSortable": false },
                    { "mData": "Address", "bSortable": false },
                    { "mData": "ContactName", "bSortable": false },
                    { "mData": "ContactPhone", "bSortable": false }
                ]
            });
            $("#tabServiceProviderBranch")
                .dataTable({
                    "sAjaxSource": "/WorkOrderCommonInfo/ServiceProviderBranchList?AccessoriesTypeID=" + $("#AccessoriesTypeID").val() + "&AccessoriesLevelID=" + $("#AccessoriesLevelID").val() + "&provinceId=" + $("#ProviderBranchProvinceID").val() + "&CityId=" + $("#ProviderBranchCityID").val() + "&ServiceType=" + $("#WarrantyMaintenanceTypeID").val() + "&ProductModelId=@Model.InsuranceOrder.ProductModelId",
                    aoColumns: [
                        {
                            "mData": null,
                            "bSortable": false,
                            "fnRender": function (nRow) {
                                return '<input type="radio" name="ServiceProviderBranch" class="CheckBoxItem" ' + ("@(Model.ServiceProviderBranchId)" == nRow.aData.ServiceProviderBranchID
                                    ? "checked" : "") + ' value="' + nRow.aData.ServiceProviderBranchID + '" onclick="checkServiceBranch(this)"/>';
                            }
                        },
                        { "mData": "ServiceProviderName", "bSortable": false },
                        { "mData": "ServiceProviderBranchName", "bSortable": false },
                        { "mData": "QuotationPrice", "bSortable": false },
                        { "mData": "ProvinceName", "bSortable": false },
                        { "mData": "CityName", "bSortable": false },
                        { "mData": "Address", "bSortable": false }
                    ]
                });
        });
        $("#WarrantyMaintenanceTypeID").change(function () {
            changeMaintenanceType(this);
            searchProviderBranch();
        });
        function changeMaintenanceType(obj) {
            var warrantyMaintenanceType = $(obj).val() - 0;
            if (warrantyMaintenanceType == "@((byte)WarrantyMaintenanceType.SendRevised)") {
                $("#sortingCenterMain").show();//分拣中心信息
                $("#CallCurier").show();//呼叫快递
                $("#userWarrantyRemarkMain").hide();//维修备注
                $("#serviceProviderBranchMain").hide();//服务网点
            } else if (warrantyMaintenanceType == "@((byte)WarrantyMaintenanceType.OfficialRepair)") {
                $("#sortingCenterMain").hide();
                $("#CallCurier").hide();
                $("#userWarrantyRemarkMain").show();
                $("#serviceProviderBranchMain").hide();
            } else {
                $("#sortingCenterMain").hide();
                $("#CallCurier").hide();
                $("#userWarrantyRemarkMain").show();
                $("#serviceProviderBranchMain").show();
            }
        }

        //保存信息
        function Save(saveType) {
            var fromExpressInfo = $('#fromExpressInfo').serializeJson();//快递信息
            var param = $.extend({}, fromExpressInfo);//多个表单参数拼接


            var warrantyMaintenanceType = $("#WarrantyMaintenanceTypeID").val() - 0;
            var accidentAddress = $("#AccidentAddress").val();
            var obj = $("#tabServiceProviderBranch").find(":radio:checked");
            var serviceProviderBranchId = $("#ServiceProviderBranchId").val();
            var serviceProviderName = $(obj).parent().siblings().eq(0).html();
            var serviceProviderBranchName = $(obj).parent().siblings().eq(1).html();
            var quotationPrice = $(obj).parent().siblings().eq(2).html();
            var userWarrantyRemark = $("#UserWarrantyRemark").val();
            var accessoriesTypeID = $("#AccessoriesTypeID").val();
            var accessoriesLevelID = $("#AccessoriesLevelID").val();

            param.Status = "@Model.Status";
            param.WarrantyMaintenanceTypeID = warrantyMaintenanceType;
            param.AccidentAddress = accidentAddress;
            param.ServiceProviderBranchId = serviceProviderBranchId;
            param.ServiceProviderName = serviceProviderName;
            param.ServiceProviderBranchName = serviceProviderBranchName;
            param.UserWarrantyRemark = userWarrantyRemark;
            param.AccessoriesTypeID = accessoriesTypeID;
            param.AccessoriesLevelID = accessoriesLevelID;

            if (warrantyMaintenanceType == "@((byte)WarrantyMaintenanceType.OnsiteRepair)" || warrantyMaintenanceType == "@((byte)WarrantyMaintenanceType.ShopRepair)") {
                if (serviceProviderBranchId == "") {
                    layer.alert("请选择维修服务商");
                    return;
                }
                if (parseFloat(quotationPrice) > parseFloat("@Model.InsuranceOrder.InsuredAmount")) {//验证报价是否超过保额
                    layer.alert("维修报价大于保额@(Model.InsuranceOrder.InsuredAmount)，请选择其他维修商");
                    return;
                }
            }
            //验证物流信息
            if (warrantyMaintenanceType == "@((byte)WarrantyMaintenanceType.SendRevised)" &&
                !$("#fromExpressInfo").valid()) {
                layer.alert("请填写完整物流信息！");
                return;
            }
            layer.load();
            $.ajax({
                url: "/InMaintainWorkOrder/SaveSendOrderAgain?ID=@Model.ID",
                type: "Post",
                data: param,
                success: function (data) {
                    layer.alert(data.Message, {
                        end: function () {
                            layer.closeAll();
                            if (data.Success) {
                                layer.load();
                                location.reload();
                            }
                        },
                        yes: function () {
                            layer.closeAll();
                            if (data.Success) {
                                layer.load();
                                location.reload();
                            }
                        }
                    });
                }
            });
        }
    </script>
}
<div class="form-actions text-center">
    <input class="btn btn-primary Edit-R" type="button" onclick="Save()" value="确定">
    <input class="btn" type="button" value="取消" onclick="parent.layer.closeAll()">
</div>
